package sisi.suppliers;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * SupplierproductController.java - Controller class for the <i>supplier products database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 10/12/2013
 * 
 */

public class SupplierproductController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Supplierproduct[] get(String cerca) {
		List<Supplierproduct> lista = getList(cerca);
			return lista.toArray(new Supplierproduct[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Supplierproduct> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.prodname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Supplierproduct as c " + cWhere 
					+ " order by c.prodname ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Supplierproduct> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Supplierproduct> getListxIdSupplier(Integer id) {

		String cWhere = "";
		cWhere = " WHERE c.supid = :cerca ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Supplierproduct as c " + cWhere 
					+ " order by c.seq ");

			q.setParameter("cerca", id );

			List<Supplierproduct> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Supplierproduct> getListxIdProduct(Integer idProduct) {

		String cWhere = "";
		cWhere = " WHERE c.prodid = "+idProduct;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Supplierproduct as c " + cWhere 
					+ " order by c.seq ");

			List<Supplierproduct> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Supplierproduct getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Supplierproduct supplierproductx = em.find(Supplierproduct.class, id);
			return supplierproductx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Supplierproduct add(Supplierproduct supplierproduct) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(supplierproduct);
			em.getTransaction().commit();
			supplierproduct = refresh(supplierproduct);
		} finally {
			em.close();
			return supplierproduct;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Supplierproduct supplierproduct) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Supplierproduct supplierproductx = em.find(Supplierproduct.class, supplierproduct.getId());
			supplierproductx.setId(supplierproduct.getId());
			supplierproductx.setSeq(supplierproduct.getSeq());
			supplierproductx.setDtup(supplierproduct.getDtup());
			supplierproductx.setLogicDelete(supplierproduct.getLogicDelete());
			supplierproductx.setSupname(supplierproduct.getSupname());
			supplierproductx.setProdid(supplierproduct.getProdid());
			supplierproductx.setProdname(supplierproduct.getProdname());
			supplierproductx.setSupid(supplierproduct.getSupid());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Supplierproduct refresh(Supplierproduct supplierproduct) {

		Supplierproduct supplierproductx = supplierproduct;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			supplierproductx = em.find(Supplierproduct.class, supplierproduct.getId());
			em.flush();
			em.refresh(supplierproductx);
		} finally {
			em.close();
			return supplierproductx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Supplierproduct supplierproduct) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Supplierproduct supplierproductx = em.find(Supplierproduct.class, supplierproduct.getId());
			em.remove(supplierproductx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
